<?php 
declare(strict_types=1);
namespace App\Service;

use App\Dao\LotteryDao;
use App\Dao\WxuserDao;
use App\Exception\BusinessException;
use Hyperf\Di\Annotation\Inject;
use App\Constants\ErrorCode;
use App\Utils\ServerUtil;
use App\Dao\LotteryPrizeDao;
use App\Dao\AdminLogDao;

class LotteryService
 { 

   /**
     * @Inject
     * @var AdminLogDao
     */
    protected $adminLogLogic;

    /**
     * @Inject
     * @var LotteryPrizeDao
     */
    protected $lotteryPrizeDao;

    //获取抽奖次数列表
    public function list($nickname,$where,$pageSize)
    {
        return $this->lotteryPrizeDao->getUserListPage($nickname,$where,$pageSize);
    }

    /**
     * 系统设置-奖品列表
     * @Author czm
     * @DateTime 2020-04-17
     * @return void
     */
    public function prize()
    {
        return $this->lotteryPrizeDao->getAllByWhere();
    }

    /**
   * 抽奖记录导出
   * @Author czm
   * @DateTime 2020-06-04
   * @param [type] $start_time
   * @param [type] $end_time
   * @return string
   */
  public function outputLog($map){

    $head=['id','奖品id','openid','备注','备注说明','订单号','操作时间','剩余抽奖次数','类型','抽奖次数'];
    return $this->lotteryPrizeDao->outputLog($map,$head);
  }

    /**
     * 系统设置-奖品列表-奖品新增
     * @Author czm
     * @DateTime 2020-04-17
     * @return void
     */
    public function prizeAdd($params)
    { 
      $this->lotteryPrizeDao->addTrait($params);
      $this->adminLogLogic->addLog('添加了抽奖物品'.json_encode($params,JSON_UNESCAPED_UNICODE));
    }

    /**
     * 系统设置-奖品列表-奖品编辑
     * @Author czm
     * @DateTime 2020-04-17
     * @return void
     */
    public function prizeEdit($params)
    {
      $logInfo=$this->lotteryPrizeDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE);
      $this->lotteryPrizeDao->updateTrait($params);
      $this->adminLogLogic->addLog('编辑抽奖物品'.$logInfo.'编辑后'.$this->lotteryPrizeDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE));
    }


    /**
     * 系统设置-奖品列表-奖品删除
     * @Author czm
     * @DateTime 2020-04-17
     * @return void
     */
    public function prizeDel($params)
    {
      $logInfo=$this->lotteryPrizeDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE);
      $this->lotteryPrizeDao->updateTrait(['id'=>$params['id'],'is_del'=>2]);
      $this->adminLogLogic->addLog('删除了抽奖物品'.$logInfo);
    }

    /**
     * 新增用户和用户上级抽奖机会
     * @param $openid
     * @return bool
     */
    public function addTierLottery($openid, $orderNo)
    {
        $user = make(WxuserDao::class)->getUser($openid, '*');
        if (empty($user)) {
            return false;
        }

        $lotteryDao = make(LotteryDao::class);
        if ($lotteryDao->findByWhere(['order_no' => $orderNo])) {
            return true;
        }
        $data = [
            'openid' => $openid,
            'remark' => '话费充值成功',
            'remark_desc' => '',
            'count' => 1,
            'type' => 4,
            'order_no' => $orderNo
        ];

        $user = json_decode(json_encode($user), true);
        $lotteryDao->addLottery($data);

        if (!empty($user['pid'])) {
            $data['type'] = 5;
            $data['openid'] = $user['pid'];
            $data['remark'] = '';

            $lotteryDao->addLottery($data);
        }
    }



 }